package com.gtl.meishi.ui;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.gtl.meishi.R;
import com.gtl.meishi.bean.User;

import cn.bmob.sms.BmobSMS;
import cn.bmob.sms.bean.BmobSmsState;
import cn.bmob.sms.listener.QuerySMSStateListener;
import cn.bmob.sms.listener.RequestSMSCodeListener;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class RegistActivity extends AppCompatActivity {
    public static String APPID = "4385626f2be1bdde82da2e6ad2cebc0e";
    private  String  name;
    private String  password;
    private EditText mEditTextName,mEditTextPassword;
    int smsId=-1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        //初始化
        init();



    }

    /**
     * 初始化
     */
    private void init() {
        Bmob.initialize(this, APPID);	//
       BmobSMS.initialize(this,APPID);

        mEditTextName= (EditText) findViewById(R.id.edittext_name_regist);
        mEditTextPassword= (EditText) findViewById(R.id.edittext_pwd_regist);
    }

    public  void  doClick(View view) {

        switch (view.getId()){
            case R.id.button_sendcode_regist:

                String phonNumber=mEditTextName.getText().toString();
                requestCode(phonNumber);
                break;
            case R.id.button_send_regist_regist:
                User u=new User();
                u.setName(mEditTextName.getText().toString());
                u.setPassword(mEditTextPassword.getText().toString());

                break;
        }
    }

    public  void  toast(String msg){
        Toast.makeText(RegistActivity.this,msg,Toast.LENGTH_SHORT).show();
    }

    /**
     * @param phonNumber
     *
     * 发送验证码
     */
   public  void requestCode(String phonNumber){

       BmobSMS.requestSMSCode(this, phonNumber, "meishi",new RequestSMSCodeListener() {
           @Override
           public void done(Integer integer, cn.bmob.sms.exception.BmobException e) {
               // TODO Auto-generated method stub
               if(e==null){//验证码发送成功
                   Log.i("bmob", "短信id："+integer);//用于查询本次短信发送详情
                   smsId=integer;
               }
           }

       });
   }

    /**
     * @param smsId
     * 验证短信
     */
    public void queryCode(int smsId, final User user){
        BmobSMS.querySmsState(RegistActivity.this, smsId, new QuerySMSStateListener() {

            @Override
            public void done(BmobSmsState bmobSmsState, cn.bmob.sms.exception.BmobException e) {
                // TODO Auto-generated method stub
                if(e==null){
                    Log.i("bmob","短信状态："+bmobSmsState.getSmsState()+",验证状态："+bmobSmsState.getVerifyState());

                    if(bmobSmsState.getVerifyState().equals("true")){
                        regist(user);

                    }
                }
            }

        });
    }

    /**
     * @param user
     * 注册
     */
    public void  regist(User user){
        user.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                if(e==null){
                    toast("创建数据成功：" + s);
                }else{
                    Log.i("bmob","失败："+e.getMessage()+","+e.getErrorCode());
                }
            }
        });

    }

}

